package com.health.config;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public AdminRealm adminRealm(){
        // MD5加密
        AdminRealm adminRealm = new AdminRealm();
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("MD5");
        adminRealm.setCredentialsMatcher(matcher);
        return adminRealm;
    }

    @Bean
    public SecurityManager securityManager(AdminRealm adminRealm,EhCacheManager cacheManager){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(adminRealm);
        defaultWebSecurityManager.setCacheManager(cacheManager);
        return defaultWebSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager);
        factoryBean.setLoginUrl("/login.html");
        Map<String,String> urlMaps = new HashMap<>();
        urlMaps.put("/admin/**", "anon");
        urlMaps.put("/appointment/**", "anon");
        urlMaps.put("/appointmentSetting/**", "anon");
        urlMaps.put("/consultingroom/**", "anon");
        urlMaps.put("/disease/**", "anon");
        urlMaps.put("/hospital/**", "anon");
        urlMaps.put("/meal/**", "anon");
        urlMaps.put("/tPermision/**", "anon");
        urlMaps.put("/upload/**", "anon");
        urlMaps.put("/registerOrder/**", "anon");
        urlMaps.put("/physicalOrder/**", "anon");
        urlMaps.put("/tRole/**", "anon");
        urlMaps.put("/tDepartment/**", "anon");
        urlMaps.put("/user/**", "anon");
        urlMaps.put("/bannedUsers/**", "anon");

        urlMaps.put("/**", "authc");
        factoryBean.setFilterChainDefinitionMap(urlMaps);
        return factoryBean;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor attributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator proxyCreator(){
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    @Bean
    public EhCacheManager ehCacheManager(){
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
        return ehCacheManager;
    }
}
